package com.itheima.service.imp;

import com.itheima.common.PageBean;
import com.itheima.dto.ClazzDto;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.service.ClazzService;
import com.itheima.vo.ClazzVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    ClazzMapper clazzMapper;
    @Override
    public PageBean list(ClazzDto clazzDto) {
        PageBean pageBean = new PageBean();
        pageBean.setTotal(clazzMapper.count(clazzDto));
        clazzDto.setPage(clazzDto.getPageSize()*(clazzDto.getPage()-1));
        List<ClazzVo> list=clazzMapper.list(clazzDto);
        for(ClazzVo clazz:list){
            if(clazz.getBeginDate().compareTo(LocalDate.now())>0){
                clazz.setStatus("未开始");
            }else if(clazz.getEndDate().compareTo(LocalDate.now())<0){
                clazz.setStatus("已结束");
            }else if(clazz.getEndDate().compareTo(LocalDate.now())>0){
            clazz.setStatus("进行中");
            }
        }
        pageBean.setRows(list);
        return pageBean;
    }

    @Override
    public void add(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        log.info("添加班级信息：{}", clazz);
        clazzMapper.insert(clazz);

    }

    @Override
    public Clazz getById(Integer id) {
        return clazzMapper.getById(id);
    }

    @Override
    public void updateById(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        log.info("修改班级信息：{}", clazz);
        clazzMapper.updateById(clazz);
    }

    @Override
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);
    }

    @Override
    public List<Clazz> listall() {
        return clazzMapper.listall();
    }
}
